import GameInfo from './GameInfo';

class ScorePanel {
    score = 0;
    level = 1;
    scoreEle : HTMLElement;
    levelEle : HTMLElement;
    gameInfo : GameInfo;

    // 设置最大等级限制
    maxLevel : number;

    private onLevelUp : Function;

    constructor(onLevelUp:Function, gameInfo:GameInfo, maxLevel : number = 10){
        this.maxLevel = maxLevel;
        this.scoreEle = document.getElementById("score")!;
        this.levelEle = document.getElementById("level")!;
        this.onLevelUp = onLevelUp;
        this.gameInfo = gameInfo;
    }

    /**
     * 分数自增，并根据分数自动升级
     */
    addScore(){
        this.score ++;
        this.scoreEle.innerText = this.score + '';
        this.gameInfo.appendLog(`又得一分，现在总分：${this.score}`);
        // 根据分数来自动升级
        if(this.score%(3*this.level)===0){
            console.log(`${this.score}*******${3*this.level}`);
            this.levelUp();
        }
    }

    levelUp(){
        if(this.level < this.maxLevel){
            this.levelEle.innerText = ++this.level + '';
            this.onLevelUp();
            this.gameInfo.appendLog(`又升一级，现在等级：${this.level}`);
        }
    }

    reset(){
        this.levelEle.innerText = '1';
        this.scoreEle.innerText = '0';
        this.score = 0;
    }
}

export default ScorePanel;